<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <!--{if $operFlg == 2}-->
    <title>新建入库单</title>
    <!--{else}-->
    <title>修改入库单</title>
    <!--{/if}-->
    <style>
        .clf{clear:both;}
        .fll{float:left;}
    </style>
</head>
<body>
<div style="margin: 10px;">

    <div class="panel panel-default">
        <div class="panel-heading">订单信息</div>
        <div class="panel-body form-horizontal">

            <div class="form-group">

                <label for="bill_no" class="col-sm-2 control-label">
                    <span class="red">*</span>
                    订单号
                </label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="bill_no" placeholder="订单号" style="width:200px;">

                </div>
            </div>

            <div class="form-group">
                <label for="bill_goods_no" class="col-sm-2 control-label">
                    <span class="red">*</span>
                    订单商品编号
                </label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="bill_goods_no" placeholder="订单商品编号" style="width:200px;">

                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input type="button" value="确定" class="btn btn-primary"  id="addLine" >
                </div>
            </div>

        </div>
    </div>

    <div class="panel panel-default">

        <div class="panel-heading">入库单商品</div>

        <table class="table table-bordered">
            <thead>
            <tr>
                <th width="100">订单号</th>
                <th width="100">订单商品编号</th>
                <th width="70">商品编号</th>
                <th width="*">商品名称</th>
                <th width="100">商品规格</th>
                <th width="60">销售价</th>
                <th width="40">&nbsp;</th>
            </tr>
            </thead>
            <tbody id="goods_list"></tbody>
        </table>

    </div>


    <div class="panel panel-default" style="margin-top: 10px;">
        <div class="panel-heading">入库单信息</div>
        <div class="panel-body form-horizontal">
            
            <div class="form-group">
                <label for="voucherType" class="col-sm-2 control-label">入库单类型</label>
                <div class="col-sm-10">
                    <select class="form-control w150" id="voucherType" name="voucherType">
                        <option value="1">生产入库</option>
                        <option value="2">退货入库</option>
                        <option value="3">其他入库</option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label for="storeID" class="col-sm-2 control-label">仓库</label>
                <div class="col-sm-10">
                    <select class="form-control w150" id="storeID" name="storeID">
                        <!--{foreach from=$store_array item=s_l}-->
                            <option value="<!--{$s_l['storeID']}-->"><!--{$s_l['storeName']}--></option>
                        <!--{/foreach}-->
                    </select>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input type="button" class="btn btn-primary" onclick="putInStore.add();"  value="确定">&nbsp;&nbsp;
                    <input type="button" class="btn btn-primary" onclick="E.closeWindows();" value="关闭" />
                </div>
            </div>

        </div>
    </div>

</div>

<script type="text/javascript">

    //页面默认选中条码
    $("#bill_no").focus();

    //订单号回车事件
    $("#bill_no").keydown(function(e){
        if(e.keyCode == 13){
            var bill_no = $.trim($("#bill_no").val());
            if(bill_no == ''){
                global_focus = 'bill_no';
                E.alert('请输入订单号',1,"putInStore.focus");
                return false;
            }else{
                $("#bill_goods_no").focus();
            }
        }
    });

    //订单商品编号回车事件
    $("#bill_goods_no").keydown(function(e){
        if(e.keyCode == 13){
            var bill_no = $.trim($("#bill_no").val());
            var bill_goods_no = $.trim($("#bill_goods_no").val());
            if(bill_no == ''){
                global_focus = 'bill_no';
                E.alert('请输入订单号',1,"putInStore.focus");
                return false;
            }
            if(bill_goods_no == ''){
                global_focus = 'bill_goods_no';
                E.alert('请输入订单商品编号',1,"putInStore.focus");
                return false;
            }

            //校验有没有重复入库商品
            if(!$.isEmptyObject(putInStore.goods)){
                for(v in putInStore.goods){
                    if(bill_goods_no == v){
                        global_focus = 'bill_no';
                        E.alert('该商品已录入');
                        $('#bill_goods_no').val('');
                        $('#bill_no').val('').focus();

                        return false;
                    }
                }
            }
            jquery_ajax(1,{bill_no: bill_no,bill_goods_no: bill_goods_no},putInStore.getGoodsInfoCallBack);
        }
    });

    // 点击确定添加一行记录
    $(document).on("click","#addLine",function(){
        var bill_no = $.trim($("#bill_no").val());
        var bill_goods_no = $.trim($("#bill_goods_no").val());
        if(bill_no == ''){
            global_focus = 'bill_no';
            E.alert('请输入订单号',1,"putInStore.focus");
            return false;
        }
        if(bill_goods_no == ''){
            global_focus = 'bill_goods_no';
            E.alert('请输入订单商品编号',1,"putInStore.focus");
            return false;
        }

        //校验有没有重复入库商品
        if(!$.isEmptyObject(putInStore.goods)){
            for(v in putInStore.goods){
                if(bill_goods_no == v){
                    global_focus = 'bill_no';
                    E.alert('该商品已录入');
                    $('#bill_goods_no').val('');
                    $('#bill_no').val('').focus();

                    return false;
                }
            }
        }
        jquery_ajax(1,{bill_no: bill_no,bill_goods_no: bill_goods_no},putInStore.getGoodsInfoCallBack);
    });
    var putInStore = {

        //扫描|输入后存放的商品信息
        goods : {

        },

        getGoodsInfo: function() {

        },


        //根据条码获取商品信息成功回调
        getGoodsInfoCallBack : function (response){

            if (response.code == 200) {

                var html = '<tr class = "trData" id="goods_line_' + response.data.bill_goods_no + '">';
                html += '<td style="display: none">' + response.data.goods_goodsID + '</td>';
                html += '<td style="display: none">' + response.data.goods_postID + '</td>';
                html += '<td>' + response.data.bill_no + '</td>';
                html += '<td>' + response.data.bill_goods_no + '</td>';
                html += '<td>' + response.data.product_code + '</td>';
                html += '<td>' + response.data.goods_name + '</td>';
                html += '<td>' + response.data.goods_spec + '</td>';
                html += '<td>' + response.data.salePrice + '</td>';
                html += '<td><a href="javascript: void(0);" onclick="putInStore.del(\'' + response.data.bill_goods_no.toString() + '\');">删除</a></td>';
                html += '</tr>';

                putInStore.goods[response.data.bill_goods_no] = {
                    bill_no: response.data.bill_no,
                    bill_goods_no: response.data.bill_goods_no
                }

                $('#goods_list').prepend( html );
                $('#bill_goods_no').val('');
                $('#bill_no').val('').focus();

            } else {

                E.alert(response.message);

            }

        },


        //点击删除
        del: function( bill_goods_no ) {
            $('#goods_line_' + bill_goods_no).remove();
            delete putInStore.goods[bill_goods_no];
        },

        //点击保存入库
        add : function (){

            //检查是否添加商品
            if($.isEmptyObject(putInStore.goods)){
                E.alert('请添加商品');
                return false;
            }

            E.ajax_post({
                url: "/invokingAjax-backend/wms/putInStore.ajax?operFlg=2",
                dataType: "json",
                data: {
                    goods: putInStore.goods,
                    voucherType: $('#voucherType').val(),
                    storeID: $('#storeID').val()
                },
                call: "putInStore.addCallBack"
            });
        },


        //保存ajax回调
        addCallBack : function (response){
            if(response.code != 200){
                E.alert(response.message);
            }else{
                if(response.code != 200){
                    E.alert(response.message);
                }else{
                    E.opener("putInStore.load");
                    E.alert('入库单保存成功',2,"E.refresh");
                }
            }
        },

        //刷新当前页面
        reload : function(){
            window.location.href = "/quanliadmin/app/wms/warehouseManage.pscript?operFlg=2";
        },

        //根据ID选中焦点
        focus : function(){

            if( global_focus != '' ){
                $("#"+global_focus).focus();
            }

        },

        //清除输入框内的值
        clear : function(){
            $("#goodsInfo").html('请先扫描或录入商品SKU');
            $(".form-control").val('');
        }

    }


    function jquery_ajax(flg,data,func){
        $.ajax({
            type: "post",
            url: "/invokingAjax-backend/wms/putInStore.ajax?operFlg="+flg,
            dataType: "json",
            data: data,
            success: function( result_obj ) {
                func(result_obj);
            },
            error : function(msg){
                E.alert('网络不给力，请重新尝试...',1);
            }
        });
    }

</script>

</body>

</html>
